package com.hxd.otc.module.my.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.enums.CountryCodeEnum;
import com.hxd.otc.module.my.activity.RealNameStep1Activity;
import com.hxd.otc.module.my.bean.UploadImageBean;
import com.hxd.otc.module.my.contract.RealNameStep1Contract;
import com.hxd.otc.module.my.model.UploadImageModel;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;

import java.io.File;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class RealNameStep1Presenter extends BasePresenter<RealNameStep1Contract.IView> implements RealNameStep1Contract.IPresenter {

    private final UploadImageModel mUploadImageModel;
    private final UserModel mUserModel;
    private CountryCodeEnum mRealNameType;
    private HashMap<String, Object> mRealNameInfo;

    public RealNameStep1Presenter(RealNameStep1Contract.IView iView) {
        super(iView);
        mUploadImageModel = UploadImageModel.getInstance();
        mUserModel = UserModel.getInstance();
    }


    @Override
    public void uploadImage(final String tag, File file) {
        mUploadImageModel.uploadCardImage(file, new Observer<String>() {

            @Override
            public void onError(Throwable e) {
                mView.uploadImageFail(tag,e.getMessage());
            }

            @Override
            public void onComplete() {
            }

            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(String result) {
                mView.uploadImageSuccess(tag,result);

            }
        },mView.<String>bindAutoDispose());
    }

    @Override
    public void setRealNameType(CountryCodeEnum realNameType) {
        mRealNameType = realNameType;
    }

    @Override
    public void setRealNameInfo(HashMap<String, Object> realNameInfo) {
        mRealNameInfo = realNameInfo;
    }

    @Override
    public void realNameVerify(HashMap<String, UploadImageBean> imageMaps) {

        String cardUpId = imageMaps.get(RealNameStep1Activity.IMAGE_TAG_FACE).getRemotePath();
        String cardDownId = imageMaps.get(RealNameStep1Activity.IMAGE_TAG_BACK).getRemotePath();
        String cardFaceId = imageMaps.get(RealNameStep1Activity.IMAGE_TAG_HOLD).getRemotePath();
        BaseObserver<String> baseObserver = new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
                mView.realNameVerifySuccess();
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        };
        //实名分两种情况，一种是只进行Lv2认证，一种是完整的认证（区分在认证的基本信息）
        if (mRealNameInfo == null || mRealNameInfo.size() == 0) {
            mUserModel.realNameVerifyOfV2(cardUpId, cardDownId, cardFaceId, baseObserver, mView.<BaseBean<String>>bindAutoDispose());
        } else {
            String idCardNo = (String) mRealNameInfo.get("idCardNo");
            String age = (String) mRealNameInfo.get("age");
            int isMale = (int) mRealNameInfo.get("isMale");
            String name = (String) mRealNameInfo.get("name");
            String address = (String) mRealNameInfo.get("address");
            mUserModel.realNameVerify(idCardNo, age, isMale, name, address, mRealNameType, cardUpId, cardDownId, cardFaceId, baseObserver, mView.<BaseBean<String>>bindAutoDispose());
        }
    }

    public CountryCodeEnum getRealNameType() {
        return mRealNameType;
    }

    public HashMap<String, Object> getRealNameInfo() {
        return mRealNameInfo;
    }
}
